package com.weaver.drools.common.utils;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 
*  @application name：字符串转换工具类 
*  @author: zhouxinlei
*  @time：2018年7月18日
*  @version：ver 1.1
 */
public class StringUtilFomat {
	
    public static final String dateFormat_yyyy_MM_dd = "yyyy-MM-dd";
    public static final String dateFormat_yyyyMM = "yyyyMM";
    public static final String dateFormat_dd = "dd";
    public static final String dateFormat_yyyyMMdd = "yyyyMMdd";
    public static final String dateFormat_yyyy_MM_dd_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String dateToString(Date date, String dateFormat) {
        String str = null;
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        if (date == null) {
            return "";
        } else {
            str = format.format(date);
            return str;
        }
    }

    public static Date stringToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        java.sql.Date date = null;

        try {
            format.parse(str);
        } catch (ParseException arg3) {
            arg3.printStackTrace();
        }

        date = java.sql.Date.valueOf(str);
        return date;
    }

    public static String ChineseGarbled(String needFormatStr, String CodingFormat) {
        String ChineseGarbled = "";

        try {
            ChineseGarbled = new String(needFormatStr.getBytes("iso8859-1"), CodingFormat);
        } catch (UnsupportedEncodingException arg3) {
            arg3.printStackTrace();
        }

        return ChineseGarbled;
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();
    }

    public static String formatNumeric(Double number) {
        DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
        df.setGroupingUsed(false);
        return df.format(number);
    }

    public static String formatNumericCurrency(Double number) {
        DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
        return df.format(number);
    }
}
